/**
* @{#} ${ classPre }Service.java Created on ${ date }
*
* Copyright (c) ${ year } by hiwitech.com software.
*/
package ${packageName}.service.${moduleName}.impl;

import ${packageName}.common.error.BizException;
import ${packageName}.common.restapi.ResultCodeEnum;
import ${packageName}.mapper.BaseMapper;
import ${packageName}.mapper.${ classPre }MapperEx;
import ${packageName}.model.request.common.CountQuery;
import ${packageName}.model.db.${ classPre };
import ${packageName}.model.request.${moduleName}.${ classPre }Request;
import ${packageName}.model.vo.${ classPre }VO;
import ${packageName}.service.BaseServiceImpl;
import ${packageName}.service.${moduleName}.${ classPre }Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hiwitech
 * @version 1.0
 */
@Service("${ classPre?uncap_first }Service")
public class ${ classPre }ServiceImpl extends BaseServiceImpl<${ classPre }, ${ classPre }VO> implements ${ classPre }Service {

	@Autowired
	private ${ classPre }MapperEx ${ classPre?uncap_first }Mapper;

    @Override
    public BaseMapper getMapper() {
        return ${ classPre?uncap_first }Mapper;
    }

	@Override
    public int updateById(${classPre}Request ${ firstLowerClassPre }Request) {
    	// 校验请求的基本参数
        ${ firstLowerClassPre }Request.formatCheck();
        // 验证请求的业务参数
        saveOrUpdateBussiParamCheck(${ firstLowerClassPre }Request);

        // 不能修改字段，修改前置空 防止非法请求导致值变更
        <#list tableConfig.canNotEditFields as field>
        ${ firstLowerClassPre }Request.set${fieldMap[field].firstUpperCaseProName}(null);
        </#list>

    	return super.updateById(${ firstLowerClassPre }Request);
    }

	@Override
    public int insert(${classPre}Request ${ firstLowerClassPre }Request) {
		// 校验请求的基本参数
        ${ firstLowerClassPre }Request.formatCheck();
        // 验证请求的业务参数
        saveOrUpdateBussiParamCheck(${ firstLowerClassPre }Request);

    	return super.insert(${ firstLowerClassPre }Request);
    }
    
    /**
     * 保存或修改参数时验证业务参数
     *
     * @param ${ firstLowerClassPre }Request
     */
    private void saveOrUpdateBussiParamCheck(${classPre}Request ${ firstLowerClassPre }Request) {
        // 验证失败直接抛出异常即可 throw new BizException();
        /*******************去重验证******************/
        <#list tableConfig.addFields as addField>
            <#if !fieldMap[addField].canRepeat>
        if (checkRepeat(new CountQuery(${ firstLowerClassPre }Request.getId(), "${fieldMap[addField].proName}", ${ firstLowerClassPre }Request.get${fieldMap[addField].firstUpperCaseProName}()))) {
            throw new BizException(ResultCodeEnum.ERROR_INVALID_PARAMS, "${fieldMap[addField].viewName}已存在");
        }
            </#if>
        </#list>
    }
    
	@Override
    public int saveOrUpdate(${classPre}Request ${ firstLowerClassPre }Request) {
        if (${ firstLowerClassPre }Request.isAdd()) {
            return this.insert(${ firstLowerClassPre }Request);
        } else {
            return this.updateById(${ firstLowerClassPre }Request);
        }
    }
}